package com.firemoss.atonement.mapper.resolution
{
	import com.firemoss.atonement.mapper.Mapping;
	import com.firemoss.atonement.metadata.Property;
	import flash.utils.Dictionary;
	import mx.collections.IList;
	import org.as3commons.reflect.Type;

	public class ListValueResolver implements IValueResolver
	{

		//----------------------------------------------------------
		//   Static Functions 
		//----------------------------------------------------------

		public static function resolve( source : *, targetProperty : Property, mapping : Mapping, mappingChain : Dictionary ) : *
		{
			if ( source == null )
			{
				return null
			}
			else if ( mappingChain[ source ] == null )
			{
				var targetClass : Class = targetProperty.type.clazz
				var result : IList = new targetClass()

				for each ( var sourceItem : * in source )
				{
					result.addItem( CollectionValueResolverHelper.createCollectionItem( source, sourceItem, targetProperty, mapping, mappingChain ))
				}

				mappingChain[ source ] = result

				return result
			}
			else
			{
				return mappingChain[ source ]
			}
		}

		//----------------------------------------------------------
		//   Constructor 
		//----------------------------------------------------------

		public function ListValueResolver()
		{

		}

		//----------------------------------------------------------
		// Public Methods 
		//----------------------------------------------------------

		public function resolve( source : *, targetProperty : Property, mapping : Mapping, mappingChain : Dictionary ) : *
		{
			return ListValueResolver.resolve( source, targetProperty, mapping, mappingChain );
		}
	}
}
